package net.siufung.security.granter;

import com.google.common.collect.Lists;
import net.siufung.security.base.service.ICurrentUserService;
import net.siufung.security.provider.AccessTokenAuthenticationGranter;
import net.siufung.security.provider.PasswdAuthenticationGranter;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.provider.CompositeTokenGranter;
import org.springframework.security.oauth2.provider.TokenGranter;

import java.util.List;

/**
 * @author 陈建峰
 * @date 2022/10/20 2:33 下午
 * @since V1.0.0
 */
@SuppressWarnings("deprecation")
public class DefaultTokenGranterProvider implements ITokenGranterProvider{

    @Override
    public TokenGranter getCompositeTokenGranter(AuthorizationServerEndpointsConfigurer endpoints,
                                                 ICurrentUserService currentUserService,
                                                 AuthenticationManager authenticationManager) {
        List<TokenGranter> granters = Lists.newArrayList(endpoints.getTokenGranter());
        granters.add(new AccessTokenAuthenticationGranter(
                endpoints.getTokenServices(),
                endpoints.getClientDetailsService(),
                endpoints.getOAuth2RequestFactory(),
                authenticationManager,
                currentUserService));
        granters.add(new PasswdAuthenticationGranter(
                endpoints.getTokenServices(),
                endpoints.getClientDetailsService(),
                endpoints.getOAuth2RequestFactory(),
                authenticationManager,
                currentUserService));
        return new CompositeTokenGranter(granters);
    }
}
